package gatperdut.chato.core.messaging.packagers;

import gatperdut.chato.model.Chato.Player.Player;

import java.util.Arrays;

import org.json.simple.JSONObject;

public class PlayersPackager extends GenericPackager<Player> {

    public static enum PlayerJSONAttr {
        ID("id"), USERNAME("username"), PASSWORD("password"), ADMIN("admin");

        private final String name;

        private PlayerJSONAttr( String name ) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    };

    @SuppressWarnings( "unchecked" )
    @Override
    public JSONObject packageOne( Player player ) {
        JSONObject playerJO = new JSONObject();

        playerJO.put( PlayerJSONAttr.ID.getName(), player.cdoID().toURIFragment() );
        playerJO.put( PlayerJSONAttr.USERNAME.getName(), player.getUsername() );
        playerJO.put( PlayerJSONAttr.ADMIN.getName(), player.isAdmin() );

        return playerJO;
    }

    @Override
    public Player[] toArray( Object[] playersArray ) {
        return Arrays.copyOf( playersArray, playersArray.length, Player[].class );
    }

    public JSONObject packageRegistrationResult( Player player ) {
        if ( player == null ) {
            return new JSONObject();
        }
        else {
            return packageOne( player );
        }
    }
}
